// import { Pool, PoolObjects } from './Pool';
// import { GameTime } from './GameTime';
// import {
// 	WestTimer,
// 	westCreatetimer,
// 	TimeoutCallbackFunc,
// 	TimeRepeatCallbackFunc,
// } from './WestTimer';
// 倾向于使用TimersObj而不是上面的函数！
// 当tag相同时，后一个会覆盖前一个，不会重复添加！
// 不建议继承此类，而是用组合的方法
class TimersObj extends PoolObjects {
	objs: WestTimer[] = [];

	timerNum(): number {
		return this.objs.length;
	}

	setTimeout(name: string, call: TimeoutCallbackFunc, self: unknown, ms: number): WestTimer {
		if (this.released) {
			console.error('TimersObj.setTimeout but released');
			return null;
		}
		this.clearTimer(name);
		const timer = westCreatetimer(name, ms < 0 ? 0 : ms, 1, call, self, false);
		timer.start(this.paused);
		this.objs.push(timer);
		return timer;
	}

	setInterval(
		name: string,
		call: TimeRepeatCallbackFunc,
		self: unknown,
		ms: number,
		checkReturn = false,
	): WestTimer {
		if (this.released) {
			console.error('TimersObj.setInterval but released');
			return null;
		}
		this.clearTimer(name);
		const timer = westCreatetimer(name, ms, -1, call, self, false);
		timer.checkReturn = checkReturn;
		timer.start(this.paused);
		this.objs.push(timer);
		return timer;
	}

	// will call 0..repeatcount-1
	setTimerRepeat(
		name: string,
		call: TimeRepeatCallbackFunc,
		self: unknown,
		ms: number,
		repeatCount: number,
	): WestTimer {
		if (this.released) {
			console.error('TimersObj.setTimerRepeat but released');
			return null;
		}
		this.clearTimer(name);
		if (repeatCount <= 0) {
			return null;
		}
		const timer = westCreatetimer(name, ms, repeatCount, call, self, false);
		timer.start(this.paused);
		this.objs.push(timer);
		return timer;
	}

	// will call 0..repeatcount
	setTimerRepeatNow(
		name: string,
		call: TimeRepeatCallbackFunc,
		self: unknown,
		ms: number,
		repeatCount: number,
	): WestTimer {
		const timer = this.setTimerRepeat(name, call, self, ms, repeatCount + 1);
		if (timer) {
			timer.onTimer();
			return timer;
		}
		return null;
	}

	nextFrame(name: string, call: TimeoutCallbackFunc, self: unknown): WestTimer {
		if (this.released) {
			console.error('TimersObj.nextFrame but released');
			return null;
		}
		this.clearTimer(name);
		const timer = westCreatetimer(name, 0, 1, call, self, false);
		timer.start(this.paused);
		this.objs.push(timer);
		return timer;
	}

	// will call 0..repeatcount
	setTimerNextRepeat(
		name: string,
		call: TimeRepeatCallbackFunc,
		self: unknown,
		next: number,
		ms: number,
		repeatCount: number,
	): WestTimer {
		return this.setTimeout(
			`${name}_timeout`,
			() => {
				this.setTimerRepeatNow(name, call, self, ms, repeatCount);
			},
			null,
			next,
		);
	}

	// 从现在到"15:00"，每100秒调用一次
	setTimerUntil(
		name: string,
		call: TimeRepeatCallbackFunc,
		self: unknown,
		sec: number,
		endtime: string,
	): WestTimer {
		this.clearTimer(name);
		const gametime = GameTime.ins();
		const end = gametime.getTimeToToday(endtime);
		// console.log("setTimerUntil",name,sec,endtime,end)
		if (end <= 0) {
			return null;
		}
		const minms = sec * 1000;
		const times = Math.floor(end / minms);
		const mod = end % minms;
		return this.setTimerNextRepeat(name, call, self, mod, minms, times);
	}

	// 从"12:00"到"13:00", 每60*2秒一次
	setTimerStartUntil(
		name: string,
		call: TimeRepeatCallbackFunc,
		self: unknown,
		sec: number,
		starttime: string,
		endtime: string,
	): WestTimer {
		this.clearTimer(name);
		const gametime = GameTime.ins();
		const start = gametime.getTimeToToday(starttime);
		return this.setTimeout(
			`${name}_timeout`,
			() => {
				this.setTimerUntil(name, call, self, sec, endtime);
			},
			null,
			start,
		);
	}

	clearTimer(name: string): boolean {
		// console.log("clearTime",name);
		// 允许匿名的timer！
		if (!name) {
			return false;
		}
		const t = this.objs.removeIfa((a) => a._name === name);
		if (t) {
			t.release();
			return true;
		}
		return false;
	}

	clearAll(): void {
		// console.log('clearAll', this.objs.length);
		for (const obj of this.objs) {
			obj.release();
		}
		this.objs = [];
	}

	released = false;

	release(): void {
		this.released = true;
		this.clearAll();
	}

	paused = false;

	pauseAll(): void {
		// console.log('pauseAll', this.objs.length);
		this.paused = true;
		for (const obj of this.objs) {
			obj.pause();
		}
	}

	resumeAll(): void {
		// console.log('resumeAll', this.objs.length);
		this.paused = false;
		for (const obj of this.objs) {
			obj.resume();
		}
	}

	static createTimerobj(name: string): TimersObj {
		return Pool.ins().pop(name, 'TimersObj', TimersObj);
	}
}
